package kr.couponplay.android.activity.intro.command;

import kr.couponplay.android.AppContext;
import kr.couponplay.android.Constants;
import kr.couponplay.android.support.StringUtils;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.provider.Settings.Secure;

public class SidSettingCommand implements Command {

	@Override
	public void execute(Context context) {
		SharedPreferences preferences = context.getSharedPreferences(Constants.PREFERENCE_NAME, Activity.MODE_PRIVATE);
		String sid = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
		if (StringUtils.isEmpty(sid)) {
			sid = preferences.getString(Constants.KEY_SID, makeSid());
    	}

		Editor editor = preferences.edit();
		editor.putString(Constants.KEY_SID, sid);
		editor.commit();
		AppContext.setSid(sid);
	}
	
	private String makeSid() {
		String temp1 = String.valueOf((int)(Math.random() * 10) + 1);
		String temp2 = String.valueOf(System.currentTimeMillis());
		return temp1 + "-" + temp2;
	}
}
